package com.jumi.microservice.controller;

import com.alibaba.fastjson.JSON;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.SendResult;
import com.aliyun.openservices.ons.api.bean.ProducerBean;
import com.jumi.microservice.common.base.BaseController;
import com.jumi.microservice.common.base.dto.BaseResponse;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.enumeration.CommonResponseEnum;
import com.jumi.microservice.common.exception.BaseException;
import com.jumi.microservice.constant.PayChannelEnum;
import com.jumi.microservice.dto.MqMessage;
import com.jumi.microservice.dto.PayNotifyRequest;
import com.jumi.microservice.rocketmq.RocketMqConfig;
import com.jumi.microservice.service.IPayService;
import com.jumi.microservice.service.IRocketMqService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;

/**
 * @author Dirk
 * @Date 2020-07-10 14:55
 */
@RestController
@RequestMapping("/payment")
public class PayController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(PayController.class);

    @DubboReference
    private IPayService payService;
    @DubboReference
    private IRocketMqService iRocketMqService;

    @Resource
    ProducerBean producerBean;
    @Autowired
    RocketMqConfig rocketMqConfig;

    /**
     * 支付结果回调
     *
     * @param request
     * @return
     */
    @PostMapping("/notify/union")
//    @ApiOperation("聚合支付回调")
    public BaseResponse paymentNotify(PayNotifyRequest request) {
        request.setPayChannel(PayChannelEnum.UNION_PAY.name());
        return payService.paymentNotify(request);
    }

    @PostMapping("/message")
    @ApiOperation("测试消息")
    public ResponseResult message(@ApiParam("message") String message) {
        return iRocketMqService.message(message);
    }


    @PostMapping("/test")
    @ApiOperation("测试消息")
    public void test() {
        MqMessage mqMessage = new MqMessage();
        mqMessage.setPayNo("3");
        mqMessage.setThirdPartyPaySn("8");
        try {
            byte[] bytes = JSON.toJSONString(mqMessage).getBytes(StandardCharsets.UTF_8);
            Message message = new Message(rocketMqConfig.getTopic(), rocketMqConfig.getTag(), "", bytes);
            SendResult sendResult = producerBean.send(message);
            log.info("发送消息成功[{}],[{}]", sendResult.getMessageId(), sendResult.getTopic());
        } catch (Exception e) {
            log.error("rocketMq 发送消息失败");
            throw new BaseException(CommonResponseEnum.SERVER_ERROR);
        }
    }

}
